/*
 * Copyright 2014 alexpad@gmail.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package alexpad.util;

import java.util.*;

/**
 *
 * @author alexpad@gmail.com
 */
public class StringUtil {

	protected StringUtil() {
	}
	
	public static boolean isBlank(String value) {
		if (value == null) {
			return true;
		}
		return value.trim().length()==0;
	}

	public static String changeEmpty(String value) {
		if (value == null) {
			return "";
		}
		return value;
	}

	public static String changeBlank(String value, String def) {
		if (value == null || value.trim().length() == 0) {
			return def;
		}
		return value;
	}

	public static long changeBlank(String value, long def) {
		try {
			if (value == null || value.trim().length() == 0) {
				return def;
			}
			return Long.parseLong(value);
		}
		catch (Exception e) {
			return def;
		}
	}

	public static boolean changeBlank(String value, boolean def) {
		try {
			if (value == null || value.trim().length() == 0) {
				return def;
			}
			return Boolean.parseBoolean(value);
		}
		catch (Exception e) {
			return def;
		}
	}

	public static double changeBlank(String value, double def) {
		try {
			if (value == null || value.trim().length() == 0) {
				return def;
			}
			return Double.parseDouble(value);
		}
		catch (Exception e) {
			return def;
		}
	}

	public static float changeBlank(String value, float def) {
		try {
			if (value == null || value.trim().length() == 0) {
				return def;
			}
			return Float.parseFloat(value);
		}
		catch (Exception e) {
			return def;
		}
	}

	public static int changeBlank(String value, int def) {
		try {
			if (value == null || value.trim().length() == 0) {
				return def;
			}
			return Integer.parseInt(value);
		}
		catch (Exception e) {
			return def;
		}
	}
	
	public static Integer changeBlank(String value, Integer def) {
		try {
			if (value == null || value.trim().length() == 0) {
				return def;
			}
			return Integer.parseInt(value);
		}
		catch (Exception e) {
			return def;
		}
	}

	public static String changeBlankToNull(String value) {
		if (value != null && value.trim().length() == 0) {
			return null;
		}
		return value;
	}

	public static Integer changeZeroToNull(Integer value) {
		if (value != null && value == 0) {
			return null;
		}
		return value;
	}

	public static Long changeZeroToNull(Long value) {
		if (value != null && value.intValue() == 0) {
			return null;
		}
		return value;
	}

	public static Float changeZeroToNull(Float value) {
		if (value != null && value.intValue() == 0) {
			return null;
		}
		return value;
	}

	public static Double changeZeroToNull(Double value) {
		if (value != null && value.intValue() == 0) {
			return null;
		}
		return value;
	}

	public static Integer parseIntegerFromNullable(String value) {
		if (value == null || value.equals("null")) {
			return null;
		}
		return new Integer(value);
	}

	public static Long parseLongFromNullable(String value) {
		if (value == null || value.equals("null")) {
			return null;
		}
		return new Long(value);
	}

	public static Float parseFloatFromNullable(String value) {
		if (value == null || value.equals("null")) {
			return null;
		}
		return new Float(value);
	}

	public static Double parseDoubleFromNullable(String value) {
		if (value == null || value.equals("null")) {
			return null;
		}
		return new Double(value);
	}

	public static String parseStringFromNullable(String value) {
		if (value == null || value.equals("null")) {
			return null;
		}
		return value;
	}

	public static String trim(String source, char charToTrim) {
	   int length = source.length();
	   int start = 0;

	   while ((start < length) && (source.charAt(start) == charToTrim)) {
		   start++;
	   }
	   while ((start < length) && (source.charAt(length - 1) == charToTrim)) {
		   length--;
	   }
	   return ((start > 0) || (length < source.length())) ? source.substring(start, length) : source;
   }


}
